﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using TIMETABLEASY.iCalManager;

using TIMETABLEASY.EasyCalendar.Model;
using TIMETABLEASY.EasyCalendar.Controler;

using TIMETABLEASY.TokenAuthentification;

namespace TIMETABLEASY
{
    public partial class Window1
    {
        private void display_items_expander()
        {
            type = planning_expend_comboBox.SelectedValue.ToString();
            switch (type)
            {

                //Droits : admin
                case "University":
                    {
                        campus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        campus_label_expand.Visibility = Visibility.Hidden;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        break;
                    }

                //Droits : Admin/Manager/Teacher/Campus
                case "Campus":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        if (usertype == "administrators")
                        {

                            foreach (campus s in (from e in db_remote.campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }


                        break;
                    }

                //Droits : admin/manager/teacher/student
                case "Class":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Visible;
                        class_label_expand.Visibility = Visibility.Visible;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                                                if (usertype == "administrators")
                        {

                            foreach (campus s in (from e in db_remote.campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        break;
                    }

                //Droits : admin/manager
                case "Cursus":
                    {
                        campus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        campus_label_expand.Visibility = Visibility.Hidden;
                        cursus_expend_planning_combobox.Visibility = Visibility.Visible;
                        cursus_label_expand.Visibility = Visibility.Visible;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;


                        foreach (cursus s in (from e in db_remote.cursus select e))
                        {
                            cursus_expend_planning_combobox.Items.Add(s.name);
                        }

                        break;
                    }


                //Droits : admin/manager
                case "Period":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Visible;
                        period_label_expand.Visibility = Visibility.Visible;
                        user_expend_planning_combobox.Visibility = Visibility.Hidden;
                        user_label_expand.Visibility = Visibility.Hidden;
                        type_user_expand_combobox.Visibility = Visibility.Hidden;
                        type_user_label_expand.Visibility = Visibility.Hidden;

                        if (usertype == "administrators")
                        {

                            foreach (campus s in (from e in db_remote.campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }


                        break;
                    }


                case "User":
                    {
                        campus_expend_planning_combobox.Items.Clear();
                        campus_expend_planning_combobox.Visibility = Visibility.Visible;
                        campus_label_expand.Visibility = Visibility.Visible;
                        cursus_expend_planning_combobox.Visibility = Visibility.Hidden;
                        cursus_label_expand.Visibility = Visibility.Hidden;
                        class_expend_planning_combobox.Visibility = Visibility.Hidden;
                        class_label_expand.Visibility = Visibility.Hidden;
                        period_expend_planning_combobox.Visibility = Visibility.Hidden;
                        period_label_expand.Visibility = Visibility.Hidden;
                        user_expend_planning_combobox.Visibility = Visibility.Visible;
                        user_label_expand.Visibility = Visibility.Visible;
                        type_user_expand_combobox.Visibility = Visibility.Visible;
                        type_user_label_expand.Visibility = Visibility.Visible;


                        if (usertype == "administrators")
                        {

                            foreach (campus s in (from e in db_remote.campus select e))
                            {
                                campus_expend_planning_combobox.Items.Add(s.name);
                            }
                        }

                        else if (usertype == "managers")
                        {

                            //Récupération, découpage et affichage des campus enregistrés pour un manager
                            string[] table_campus;
                            string registred_campus = usercampus;
                            table_campus = registred_campus.Split('|');

                            for (int i = 0; i < table_campus.Length; i++)
                            {
                                campus_expend_planning_combobox.Items.Add(table_campus[i]);
                            }


                        }

                        else if (usertype == "teachers")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        else if (usertype == "students")
                        {
                            campus_expend_planning_combobox.Items.Add(usercampus);
                        }

                        break;
                    }
            }
        }

        private void display_user_type_expander()
        {
            string selected_usertype = type_user_expand_combobox.SelectedValue.ToString();
            switch (selected_usertype)
            {
                case "administrator":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (administrators s2 in (from a in db_remote.administrators select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.surname);
                    }
                    break;
                case "manager":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (managers s2 in (from a in db_remote.managers select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.surname);
                    }
                    break;
                case "teacher":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (teacher s2 in (from a in db_remote.teacher select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.surname);
                    }
                    break;
                case "student":
                    user_expend_planning_combobox.Items.Clear();
                    foreach (students s2 in (from a in db_remote.students select a))
                    {
                        user_expend_planning_combobox.Items.Add(s2.surname);
                    }
                    break;
                default:

                    break;
            }
        }

        private void submit_new_planning()
        {
            SingletonTokenUser glegle = SingletonTokenUser.GetInstance();
            glegle.TypeOfView = type;
            switch (type)
            {
                case "University":
                    break;
                case "campus":
                    glegle.User_campus = usercampus;
                    break;
                case "class":
                    glegle.User_campus = usercampus;
                    glegle.User_classe = userclass;
                    break;
                case "cursus":
                    glegle.User_cursus = usercursus;
                    break;
                case "period":
                    glegle.User_cursus = usercursus;
                    ///////////////////////////////////
                    ///// rajouter dans le singleton///
                    glegle.User_cursus = period;
                    ///////////////////////////////////
                    break;
                case "user":
                    glegle.User_campus = usercampus;
                    glegle.Type_person = usertype;
                    glegle.User_surname = username;
                    break;
                default:
                    break;
            }
        }

        private void display_campus_expander()
        {
            int id = 0;
            string campus = "default";
            try
            {
                campus = campus_expend_planning_combobox.SelectedValue.ToString();
            }
            catch { }

            class_expend_planning_combobox.Items.Clear();
            period_expend_planning_combobox.Items.Clear();


            //Actions dans le cas d'un filtre campus
            if(planning_expend_comboBox.Text == "Campus")
            {
                id = (from a in db_remote.campus where a.name == campus select a.id).FirstOrDefault();
            }
            
            //Actions dans le cas d'un filtre classe
            else if (planning_expend_comboBox.Text == "Class")
            {
                id = (from a in db_remote.campus where a.name == campus select a.id).FirstOrDefault();

                foreach (classes s2 in (from a in db_remote.classes where a.campus_id == id select a))
                {
                    class_expend_planning_combobox.Items.Add(s2.name);
                }
            }

            //Actions dans le cas d'un filtre période
            else if (planning_expend_comboBox.Text == "Period")
            {

                int current_campus_id = 0;

                current_campus_id = (from e in db_remote.campus
                                     where e.name == campus
                                     select e.id).FirstOrDefault();


                period_expend_planning_combobox.Items.Clear();

                foreach (period_campus period_campus1 in (from e in db_remote.period_campus where e.campus_id == current_campus_id select e))
                {
                    try
                    {
                        period period1 = (from e in db_remote.periods where e.id == period_campus1.period_id select e).FirstOrDefault();

                        string cursus_name = (from e in db_remote.cursus
                                             where e.id == period1.cursus_id
                                             select e.name).FirstOrDefault().ToString();

                        string period_name_all = period1.name_period;

                        string[] period_name = period_name_all.Split('?');
                        period_expend_planning_combobox.Items.Add(period_name[1] + " - cursus " + cursus_name);
                    }

                    catch (Exception)
                    {

                    }

                }
            }

            ////// fin dégueu



            //foreach (period_campus s3 in (from aaa in db_remote.period_campus where aaa.campus_id == id select aaa))
            //{
            //    foreach (period s4 in (from aaaa in db_remote.periods where aaaa.id == s3.period_id select aaaa))
            //    {
            //        period_expend_planning_combobox.Items.Add(s4.name_period);
            //    }

            //}
        }
    }
}
